package com.furongji.education.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.furongji.common.result.Result;
import com.furongji.common.utils.RedissonLockUtil;
import com.furongji.education.domain.vo.DailyStatisticsVo;
import com.furongji.education.service.DailyStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-06-01
 */
@Tag(name = "数据统计管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/statistics")
public class DailyStatisticsController {

    private final DailyStatisticsService dailyStatisticsService;

    private final RedisTemplate<String, Object> redisTemplate;

    private final ObjectMapper objectMapper;

    @Operation(summary = "数据统计")
    @GetMapping("/summary/{date}")
    public Result getSummaryStatistics(@PathVariable String date) {
        String cacheKey = "statistics:summary:" + date;
        Object cacheRaw = redisTemplate.opsForValue().get(cacheKey);

        if (cacheRaw != null) {
            DailyStatisticsVo cacheData = objectMapper.convertValue(cacheRaw, DailyStatisticsVo.class);
            return Result.success(cacheData);
        }

        DailyStatisticsVo dailyStatisticsVo = dailyStatisticsService.getSummaryStatistics(date);
        redisTemplate.opsForValue().set(cacheKey, dailyStatisticsVo, 65, TimeUnit.MINUTES);
        return Result.success(dailyStatisticsVo);
    }
}
